AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
include( "shared.lua" )

function ENT:Initialize()

	self:SetModel("models/Roller.mdl")
	
	self:PhysicsInit(SOLID_VPHYSICS)
	self:SetMoveType(MOVETYPE_VPHYSICS)
	self:SetSolid(SOLID_VPHYSICS)
	self:SetUseType(SIMPLE_USE)
	self:DrawShadow(false)
	
	self:EnableMotion( false )
end

function ENT:Think()
	
	if self.ResetTimer && CurTime() > self.ResetTimer then
		self:ResetPos()
	end
	
end

function ENT:StartResetTimer( time )
	self.ResetTimer = time
end

function ENT:StopResetTimer()
	self.ResetTimer = nil
end

function ENT:HasTimer()
	return self.ResetTimer != nil
end

function ENT:EnableMotion( state )
	local phys = self:GetPhysicsObject()
	if (phys:IsValid()) then
		phys:EnableMotion( state )
		
		if state == true then
			phys:Wake()
		end
	end
end

function ENT:Touch( ply )
	
	if ValidPlayer( ply ) then
		Flag.Pickup( ply, self )
	
	elseif ply:GetClass() == "func_disposer" then
		
		Flag.Reset( self )
		
	end
	
end

function ENT:PlayerLocalPos( ply )
	return ply:GetPos() + Vector( 0, 0, 100 )
end

function ENT:Drop()
	local parent = self:GetParent()
	local pos
	
	if ValidPlayer( parent ) then
		pos = self:PlayerLocalPos( parent )
	end

	self:EnableMotion( true )
	self:SetParent( nil )
	
	if pos then
		self:SetPos( pos )
	end
end

function ENT:Pickup( ply )
	
	self:SetParent( ply )
	self:SetPos( self:PlayerLocalPos( ply ) )
	self:EnableMotion( false )
	
end

function ENT:SetOriginProp( ent )
	self.OriginProp = ent
	
	local TeamId = ent:Team()
	
	self:SetTeam( TeamId )
	self:SetSkin( Flag.GetSkin( TeamId ) )
end

function ENT:Reset()
	
	if !ValidEntity( self.OriginProp ) then
		SafeRemoveEntity( self )
		Error("ERROR: " .. tostring(self) .. " does not have an origin prop")
	end
	
	self:SetParent( nil )
	self:EnableMotion( false )
	self:StopResetTimer()
	self:SetPos( self.OriginProp:GetPos() )
	self:SetAngles( self.OriginProp:GetAngles() )

end